package com.chance.cc.crawler.development.bootstrap.pdd.phone;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import se.vidstige.jadb.JadbConnection;
import se.vidstige.jadb.JadbDevice;

import java.io.InputStream;
import java.util.List;
import java.util.Random;

import static com.chance.cc.crawler.sheduler.job.handler.phone.AdbShellUtils.*;

/**
 * @author bx
 * @date 2021/2/2 0002 17:17
 */
public class PDDPClassifyScriptTest {
    public static Logger logger = LoggerFactory.getLogger(PDDPClassifyScriptTest.class);
    public static void main(String[] args) throws Exception {
        JadbConnection jadbConnection = new JadbConnection();
        List<JadbDevice> devices = jadbConnection.getDevices();
        int swipeNumbers = 3;
        int index =0;
        int count =3;
        Random rand = new Random();
        int num = 0;
        while (true){

            if (!checkAvailable(devices.get(0))) {
                do {
                    System.out.println("请划验证码");
                    Thread.sleep(2000);
                } while (!checkAvailable(devices.get(0)));
            }


            int step = (int)((rand.nextFloat()+1)*1000L);
//            if (num == swipeNumbers){
//                executeShell(devices.get(0),"input tap 965 1600");
//                num = 0;
//                continue;
//            }

            executeShell(devices.get(0),"input swipe 540 1900 540 0 "+step);


            index++;
            if (index > count){
                num++;
                if (!checkAvailable(devices.get(0))) {
                    do {
                        System.out.println("请划验证码");
                        Thread.sleep(2000);
                    } while (!checkAvailable(devices.get(0)));
                }
//                String[] coordinate = getCoordinate(dumpUI(devices.get(0)), "resource-id", "com.xunmeng.pinduoduo:id/tv_title");
                Elements nodes = getNodes(dumpUI(devices.get(0)), "resource-id", "com.xunmeng.pinduoduo:id/tv_title", false);
                Element element = nodes.get(nodes.size() - 1);
                String bounds = element.attr("bounds");
                if (StringUtils.isNotBlank(bounds)) {
                    String[] boundsArray = bounds.split("]");
                    int xBounds = 0;
                    int yBounds = 0;
                    for (String ba : boundsArray) {
                        String[] coordinate = ba.split(",");
                        xBounds += Integer.valueOf(coordinate[0].split("\\[")[1]);
                        yBounds += Integer.valueOf(coordinate[1]);
                    }
                    executeShell(4000,devices.get(0),"input tap "+xBounds/2+" "+yBounds/2);
                }


                for (int i = 0; i < 2; i++) {
                    if (!checkAvailable(devices.get(0))) {
                        do {
                            System.out.println("请划验证码");
                            Thread.sleep(2000);
                        } while (!checkAvailable(devices.get(0)));
                    }

                    step = (int)((rand.nextFloat()+1)*1000L);
                    executeShell(devices.get(0),"input swipe 540 1700 540 0 "+step);
                }
                executeShell(devices.get(0),"input keyevent 4");
                index =0;
                count = rand.nextInt(2)+3 ;
            }

        }

    }

    private static boolean checkAvailable(JadbDevice device) {

        boolean isAvailable = isAvailable(device);

        if (!isAvailable) { //不可用
            try {
                Thread.sleep(60000L);
            } catch (InterruptedException e) {
                logger.error(e.getMessage(), e);
            }
        }
        return isAvailable;
    }

    private static boolean isAvailable(JadbDevice device) {
        return isAvailable(device, null);
    }

    private static boolean isAvailable(JadbDevice device, InputStream inputStream) {
        String[] coordinate;
        try {
            if (inputStream == null) {
                inputStream = dumpUI(device);
            }
            coordinate = getCoordinate(inputStream, "text", "验证");
        } catch (Exception e) {
            logger.error(e.getMessage());
            return false;
        }

        if (coordinate != null) {
            logger.error("pdd find list device {} Jump out captcha!", device.getSerial());
            return false;
        }
        return true;
    }

    public static void pddClassifyList(JadbDevice device)  throws Exception {

    }

}
